package com.online.onlinestudent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.onlinestudent.dto.AjaxResult;
import com.online.onlinestudent.entity.Student;
import com.online.onlinestudent.entity.Teacher;
import com.online.onlinestudent.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public AjaxResult getAllStudents() {
        List<Student> students = studentService.list();
        if (students == null || students.isEmpty()) {
            return AjaxResult.fail("未查询到结果！");
        }
        return AjaxResult.success(students);
    }

    @GetMapping("/{id}")
    public AjaxResult getStudentById(@PathVariable Long id) {
        Student student = studentService.getById(id);
        if (student != null) {
            return AjaxResult.success(student);
        } else {
            return AjaxResult.fail("不存在该学生！");
        }
    }

    @PostMapping
    public AjaxResult saveStudent(@RequestBody Student student) {
        try {
            boolean saved = studentService.save(student);
            if (saved) {
                return AjaxResult.success("插入成功！");
            } else {
                return AjaxResult.fail("插入失败！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("插入失败，已存在该学生！");
        }
    }

    @PutMapping("/{id}")
    public AjaxResult updateStudent(@PathVariable Long id, @RequestBody Student student) {
        student.setId(id);
        try {
            boolean updated = studentService.updateById(student);
            if (updated) {
                return AjaxResult.success("更新成功！");
            } else {
                return AjaxResult.fail("更新失败，请重新输入！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteStudentById(@PathVariable Long id) {
        try {
            boolean deleted = studentService.removeById(id);
            if (deleted) {
                return AjaxResult.success("删除成功！");
            } else {
                return AjaxResult.fail("删除失败，不存在该学生！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/page")
    public AjaxResult getStudentsByPage(@RequestParam int current, @RequestParam int size) {
        try {
            Page<Student> page = studentService.page(new Page<>(current, size));
            if (page == null || page.getRecords().isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(page);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }

    @PostMapping("/username")
    public AjaxResult fuzzyQueryByUsername(@RequestBody Student student) {
        String username = student.getUsername();
        if (username == null || username.trim().isEmpty()) {
            return AjaxResult.fail("用户名不能为空！");
        }
        try {
            List<Student> list = studentService.fuzzyQueryByUsername(username);
            if (list.isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }
}